package com.rest.demo.io;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-05 16:11
 */
public class ZipoutputStreamDemo {
    public static void main(String[] args) {
        ZipOutputStream zipOutputStream = null;
        InputStream inputStream = null;
        try {
            File file = new File("F:\\a-out.zip");
            inputStream = new FileInputStream("F:\\a-out.txt");
            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            zipOutputStream.setComment("a-out");
            int b = -1;
            byte[] buff = new byte[1024];
            while((b = inputStream.read(buff)) != -1) {
                zipOutputStream.write(buff,0,b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
